package ru.arriah.servicedesk.web.commonaction;

import java.io.IOException;
import java.util.Collection;
import java.util.Properties;

import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ru.arriah.common.web.action.ActionMapping;
import ru.arriah.common.web.action.DumbAction;
import ru.arriah.servicedesk.bean.RequestBean;
import ru.arriah.servicedesk.bean.TaskBean;
import ru.arriah.servicedesk.ejb.RequestManagerLocalHome;
import ru.arriah.servicedesk.ejb.RequestManagerLocalObject;
import ru.arriah.servicedesk.help.Utils;
import ru.arriah.servicedesk.web.commonaction.exception.InternalException;

public class DisplayRequestReportFormAction  extends DumbAction {
  protected RequestManagerLocalHome requestManagerLocalHome;  
  
  public DisplayRequestReportFormAction () throws NamingException {
    super();
    
    requestManagerLocalHome = 
      Utils.getRequestManagerHomeInterface();
  }
  
  
  public DisplayRequestReportFormAction (
    RequestManagerLocalHome requestManagerLocalHome, 
    Properties config
  ) {
    super();
    this.requestManagerLocalHome = requestManagerLocalHome;
    this.setConfig(config);
  }
  

  public String execute(ActionMapping actionMapping,
      HttpServletRequest request, HttpServletResponse response)
      throws IOException, ServletException {
    try {     
     RequestManagerLocalObject requestManager = 
       requestManagerLocalHome.create();
               
     int requestId = idVal(request.getParameter("request_id"));          
     RequestBean requestBean = requestManager.selectRequestDetails(requestId);
     
     if (requestBean.getRequestStatus().getId() == 6) {       
       addErrorMessage(request, config.getProperty("ERROR.EDIT_DENIED"));       
       request.setAttribute("requestBean", requestBean);
       requestManager.remove();   
       return actionMapping.getFailureTarget();
     }
     
     Collection<TaskBean> tasksList = 
       requestManager.selectTasksByRequest(requestId);
       
     request.setAttribute("tasksList", tasksList);
     request.setAttribute("requestBean", requestBean);     
     requestManager.remove();
     
     return actionMapping.getSuccessTarget();
    } catch (Exception e) {
      handleException(e, "execute");
      throw new InternalException("ErrorMessage.internal");
    }
  }
}